#!/usr/bin/env pyahont3
import pygame
import time
import random

def draw(screen):
    pygame.draw.rect(screen,[0,0,0],[0,0,800,800],0)
    pygame.draw.rect(screen,[255,0,0],[399,399,3,3],0)
    pygame.display.flip()

g=1000
M=90
m=40
y1=-100
z1=100
x=[300,400,1,1]
clock=pygame.time.Clock()

pygame.init()
screen=pygame.display.set_mode([800,800])
screen.fill([0,0,0])

pygame.draw.rect(screen,[255,0,0],[399,399,3,3],0)
pygame.display.flip()
#pygame.draw.circle(screen, [255,255,255], [c_x2_old,c_y2_old], c_r)
running=True
while running:
    for event in pygame.event.get():
        #print(event)
        if event.type == pygame.QUIT:
            running=False
        else:
            clock.tick(20)
            draw(screen)
            pygame.draw.circle(screen, [0,255,255], [x[0],x[1]],2)
            pygame.display.flip()
            y=x[0]-400
            z=x[1]-400
            if y1+50<y or y1-50>y:
                y=y1
            if z1+50<z or z1-50>z:
                z=z1
            if y>0:
                x[2]-=g*M*m*(1/(y*y))*(1/(z*z))
                
            if y<0:
                x[2]+=g*M*m*(1/(y*y))*(1/(z*z))
                

            if z>0:
                x[3]-=g*M*m*(1/(z*z))*(1/(y*y))
            if z<0:
                x[3]+=g*M*m*(1/(z*z))*(1/(y*y))
            x[0]+=x[2]/20
            x[1]+=x[3]/20
            z1=z
            y1=y
            print(x)
"""
练习作业：04 
1. 使用pygame 显示窗口 绘制10*10方格，每个方格 50 像素宽
2. 绘制一个圆；可以用方向键控制这个圆在方格内移动
3. 存储圆的位置，在每次重新打开程序时，圆在上次关闭前的方格内


fp="python3/study/base.txt"
f=open(fp,"r+")
block_w=60
za=0
c_x=int(f.readline(100))
c_y=int(f.readline(100))
c_x2=random.randint(0,9)*60+30
c_y2=random.randint(0,9)*60+30
while c_x<=0:
    c_x+=30
while c_y<=0:
    c_y+=30

while c_x>=600:
    c_x-=120
while c_y>=600:
    c_y-=120

while (c_x-30)%60!=0:
    c_x+=1
while (c_y-30)%60!=0:
    c_y+=1
# c_y=block_w//2
c_r=(block_w-6)//2






def boo(xx,yy):
    f=block_w/2
    pygame.draw.rect(screen,[0,0,0],[xx,yy,f,f],0)
    pygame.draw.rect(screen,[255,0,0],[xx+f,yy,f,f],0)
    pygame.draw.rect(screen,[0,255,0],[xx,yy+f,f,f],0)
    pygame.draw.rect(screen,[0,0,255],[xx+f,yy+f,f,f],0)
N=10
i=0
while i<N:
    j=0
    while j<N:
        pygame.draw.rect(screen,[0,255,0],[i*block_w,j*block_w,block_w,block_w],2)
        #boo(i*block_w,j*block_w)
        j+=1
    i+=1

pygame.draw.circle(screen, [255,0,0], [c_x,c_y], c_r)
pygame.draw.circle(screen, [0,255,0], [c_x2,c_y2], c_r)
pygame.display.flip()
def is_out_range(c_x,c_y):
    if c_x<=0 or c_x>=block_w*10:
        return True
    if c_y<=0 or c_y>=block_w*10:
        return True
    return False
def is_out_ranges(c_x2,c_y2):
    if c_x2<=0 or c_x2>=block_w*10:
        return True
    if c_y2<=0 or c_y2>=block_w*10:
        return True
    return False

    
        elif event.type == pygame.KEYDOWN:
            c_x_old=c_x
            c_y_old=c_y
            c_x2_old=c_x2
            c_y2_old=c_y2
            # print(event.key)

            if event.key == pygame.K_UP:
                print("向上")
                if not is_out_range(c_x,c_y-1*block_w):                            
                    c_y-=block_w
            elif event.key == pygame.K_DOWN:
                print("向下")
                if not is_out_range(c_x,c_y+1*block_w):                            
                    c_y+=1*block_w
            elif event.key == pygame.K_LEFT:
                print("向左")
                if not is_out_range(c_x-1*block_w,c_y):                            
                    c_x-=1*block_w
            elif event.key == pygame.K_RIGHT:
                print("向右")
                if not is_out_range(c_x+1*block_w,c_y):                            
                    c_x+=1*block_w
            if c_x==c_x2 and c_y==c_y2:
                za+=1
                print(za)
                c_x2=random.randint(0,9)*60+30
                c_y2=random.randint(0,9)*60+30
                c_x=random.randint(0,9)*60+30
                c_y=random.randint(0,9)*60+30
            zx=0
            c_x3=c_x2-c_x
            c_y3=c_y2-c_y
            while zx==0:
                if c_x>=0:
                    if c_y>=0:
                        x=random.randint(0,6)
                        y=random.randint(1,3)
                    else:
                        x=random.randint(-2,6)
                        y=random.randint(-1,4)
                else:
                    if c_y>0:
                        x=random.randint(0,5)
                        y=random.randint(2,100)
                    else:
                        x=random.randint(-1,3)
                        y=random.randint(-1,3)
                if x<=0 and y<=1 and c_y2-block_w>0:
                    if not is_out_ranges(c_x2,c_y2-1*block_w):                            
                        c_y2-=block_w
                        zx=1
                        continue
                elif x>0 and x<=2 and y==3 and c_x2-block_w>0:
                    if not is_out_ranges(c_x2-1*block_w,c_y2):                            
                        c_x2-=block_w
                        zx=1
                        continue
                elif x>2 and x<=4 and y>=4 and c_y2+block_w<500:
                    if not is_out_ranges(c_x2,c_y2+1*block_w):
                        c_y2+=block_w
                        zx=1
                        continue
                elif x>4 and y>1 and y<=2 and c_x2+block_w<500:
                    if not is_out_ranges(c_x2+1*block_w,c_y2):                            
                        c_x2+=block_w
                        zx=1
                        continue
            pygame.draw.circle(screen, [255,255,255], [c_x2_old,c_y2_old], c_r)
            pygame.draw.circle(screen, [255,255,255], [c_x_old,c_y_old], c_r)
            pygame.draw.circle(screen, [0,255,0], [c_x2,c_y2], c_r)
            #boo(c_x_old-(block_w/2),c_y_old-(block_w/2))
            pygame.draw.circle(screen, [255,0,0], [c_x,c_y], c_r)
            zx=0
            if c_x==c_x2 and c_y==c_y2:
                za+=1
                print(za)
                c_x2=random.randint(0,9)*60+30
                c_y2=random.randint(0,9)*60+30
                c_x=random.randint(0,9)*60+30
                c_y=random.randint(0,9)*60+30
            pygame.display.flip()
    if za>5:
        break


f=open(fp,"w+")
s=str(c_x)
s2=str(c_y)
f.writelines([s+"\n",s2+"\n"])
f.close()
pygame.quit()
"""